﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Core.MyLinq;
using Huirui.Cavan.Core.Persistent;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Domain.MainBoundedContext.SalesModule
{
    public class ReceivablesSpecification : ISpecification<Receivables, ReceivablesSearch>
    {
        #region ISpecification<OtherReceivables,OtherReceivablesSearch> 成员

        public ReceivablesSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<OtherReceivables> 成员

        public System.Linq.Expressions.Expression<Func<Receivables, bool>> Expression
        {
            get 
            {
                IQueryBuilder<Receivables> queryBuilder = QueryBuilder.Create<Receivables>();

                if (SearchParams.IsNotNull())
                {
                    if (SearchParams.ReceivablesType > 0)
                    {
                        queryBuilder = queryBuilder.Equals(r => (int)r.ReceivablesType, SearchParams.ReceivablesType);
                    }
                    if (SearchParams.ReceivablesCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(r => r.ReceivablesCode, SearchParams.ReceivablesCode);
                    }
                    if (SearchParams.StartDate.HasValue && SearchParams.EndDate.HasValue)
                    {
                        queryBuilder = queryBuilder.Greater(r => r.CreateDate, SearchParams.StartDate.Value);
                        queryBuilder = queryBuilder.Less(r => r.CreateDate, SearchParams.EndDate.Value);
                    }
                    if (SearchParams.CustomerCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(r => r.Customer.Id, SearchParams.CustomerCode);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }

        #endregion
    }
}
